import 'package:flutter/material.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';

import 'package:get_storage/get_storage.dart';
import 'package:hc_bot_kit/service/service.dart';
import 'package:hc_bot_kit/ui/guide_setting/view.dart';
import 'package:hc_bot_pro/common/service/chat/chat.dart';
import 'package:hc_bot_pro/cure/view.dart';
import 'package:hc_bot_pro/setting/view.dart';
import 'package:hc_bot_pro/splash/view.dart';
import 'package:hc_cure_sdk_flutter/audio/service/service.dart';

import 'chat/view.dart';
import 'common/routes.dart';
import 'common/service/channel/channel.dart';
import 'common/service/chat/purpose.dart';
import 'common/service/log/api.dart';
import 'common/service/sdk_service.dart';
import 'common/service/therapy_service.dart';
import 'common/speech/service.dart';
import 'player/view.dart';

void main() async {
  try {
    await GetStorage.init();
    await HCAudioService().setUP();

    runApp(const MyApp());
  } catch (e) {
    print(e);
  }
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return ScreenUtilInit(
      designSize: const Size(960, 540),
      builder: (context, child) => GetMaterialApp(
        builder: EasyLoading.init(),
        title: 'Flutter Demo',
        defaultTransition: Transition.fadeIn,
        theme: ThemeData(
          useMaterial3: true,
          primaryColor: Color(0xff0B77E1),
          appBarTheme: AppBarTheme(
            centerTitle: true,
            titleTextStyle: TextStyle(fontSize: 15.sp, color: Colors.black),
            backgroundColor: Colors.transparent,
            //iconTheme: IconThemeData(color: Colors.white),
          ),
          scaffoldBackgroundColor: Color(0xFFF5F5F5),
          outlinedButtonTheme: OutlinedButtonThemeData(
            style: OutlinedButton.styleFrom(
              foregroundColor: Colors.white,
              side: BorderSide.none,
              backgroundColor: Color(0xff0B77E1),
              textStyle: TextStyle(color: Colors.white, fontSize: 18),
            ),
          ),
        ),
        navigatorObservers: [NavObserver()],
        initialRoute: Routes.kSplash,
        getPages: [
          GetPage(
            name: Routes.kSplash,
            page: () {
              return const SplashPage();
            },
          ),
          GetPage(
            name: Routes.kPlayer,
            page: () {
              return const PlayerPage();
            },
          ),
          GetPage(
            name: Routes.kSetting,
            page: () {
              return const SettingPage();
            },
          ),
          GetPage(
            name: Routes.kCureMenu,
            page: () {
              return const CurePage();
            },
          ),
          GetPage(
            name: Routes.kGuideSetting,
            page: () {
              return const GuideSettingPage();
            },
          ),
          GetPage(
            name: Routes.kChat,
            page: () {
              return const ChatPage();
            },
            bindings: [
              BindingsBuilder.put(() => PurposeService()),
              BindingsBuilder.put(() => ChatService()),
            ],
          ),
        ],
        initialBinding: AppBindings(),
        onInit: () async {
          await Get.find<SDKService>().initialize();
          await Get.find<CurePlayMemoService>().setUp();

          Get.find<CurePlayMemoService>().sync();
        },
      ),
      minTextAdapt: true,
      splitScreenMode: true,
    );
  }
}

class AppBindings extends Bindings {
  @override
  void dependencies() {
    Get.put<ChannelService>(ChannelService(), permanent: true);

    Get.put<SDKService>(SDKService(), permanent: true);

    Get.put<TherapyService>(TherapyService(), permanent: true);

    Get.put<CurePlayMemoService>(CurePlayMemoService(), permanent: true);

    Get.put<HCAISpeechService>(HCAISpeechService(), permanent: true);

    Get.put<RobotHardwareService>(RobotHardwareService(), permanent: true);
  }
}

class NavObserver extends NavigatorObserver {
  @override
  void didPop(Route route, Route? previousRoute) {
    super.didPop(route, previousRoute);
    debugPrint(
      "NavObserver didPop ${route.settings.name}, ${previousRoute?.settings.name}",
    );
    if (previousRoute?.settings.name == Routes.kChat) {
      Get.find<HCAISpeechService>().start();
    }
  }

  @override
  void didPush(Route route, Route? previousRoute) {
    super.didPush(route, previousRoute);
    debugPrint(
      "NavObserver didPush ${route.settings.name}, ${previousRoute?.settings.name}",
    );
    if (previousRoute?.settings.name == Routes.kChat) {
      Get.find<HCAISpeechService>().stop();
    }
  }
}
